<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">房间性质名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入房间性质名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="spaceCharacterBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="spaceCharacterBtnExport" class="layui-btn icon-btn color-green">导出</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="spaceCharacterTable" lay-filter="spaceCharacterTable"></table>
        </div>
    </div>
</div>
<style>
    .spaceCharacterForm .layui-form-label{
        width: 90px;
    }
    .spaceCharacterForm .layui-input-block{
        margin-left: 120px;
    }
</style>
<!-- 表格操作列
<script type="text/html" id="spaceCharacterTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="roomType">房间类型设置</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>-->

<!-- 表单弹窗 -->
<script type="text/html" id="spaceCharacterForm">
    <form lay-filter="spaceCharacterForm" class="layui-form model-form spaceCharacterForm">
        <input name="id" type="hidden"/>


        <div class="layui-form-item">
            <label class="layui-form-label">空间类型</label>
            <div class="layui-input-block">
                <select name="spaceId">
                    <option value="">请选择</option>
                    <option value="1">宿舍</option>
                    <option value="2">办公楼</option>
                    <option value="3">仓库</option>
                    <option value="4">写字楼</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间性质编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入房间性质编码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间性质名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入房间性质名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="spaceCharacterFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!--房间类型管理-->
<script type="text/html" id="roomTypeWindow">
    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button id="roomTypeBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button id="roomTypeBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="roomTypeTable" lay-filter="roomTypeTable"></table>
            </div>
        </div>
    </div>
</script>
<!-- 房间类型管理表格操作列 -->
<script type="text/html" id="roomTypeTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="roomTypeEdit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="roomTypeDel">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="roomTypeForm">
    <form lay-filter="roomTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">空间性质</label>
            <div class="layui-input-block">
                <select name="spaceCharacterId" id="spaceCharacterId"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间类型编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入房间类型编码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间类型名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入房间类型名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可以驻人数</label>
            <div class="layui-input-block">
                <input name="peopleNumber" placeholder="请输入可以驻人数" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="roomTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#spaceCharacterTable',
            url: config.base + 'enterprise/system/admin/spaceCharacter/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'code', sort: true, title: '房间性质编码', fixed: 'left'}
                ,{field: 'name', sort: true, title: '房间性质名称'}
                ,{field: 'spaceId', sort: true, title: '空间类型',templet:function (d) {
                        if(d.spaceId==='1'){
                            return '宿舍';
                        }else if(d.spaceId==='2'){
                            return '办公楼';
                        }else if(d.spaceId==='3'){
                            return '厂房';
                        }else if(d.spaceId==='4'){
                            return '写字楼';
                        }
                    }}
                ,{field: 'deleted', sort: true, title: '是否启用',templet:function (d) {
                        if(d.deleted){
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.deleted+'" lay-filter="deleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="deleted"   lay-text="启用|停用"/>';
                        }

                    }}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                // ,{align: 'center', toolbar: '#spaceCharacterTableBar', title: '操作', width: 300, fixed: 'right'}
            ]],
        });

        // 工具条点击事件
        table.on('tool(spaceCharacterTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            } else if (obj.event === 'roomType') { // 删除
                showRoomType(obj);
            }
        });

        //监听排序
        table.on('sort(spaceCharacterTable)', function(obj){
            table.reload('spaceCharacterTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        form.on('switch(deleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('enterprise/system/admin/spaceCharacter/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('enterprise/system/admin/spaceCharacter/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }
        // 添加按钮点击事件
        $('#spaceCharacterBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '680px',
                offset: '65px',
                title: data ? '修改空间性质' : '添加空间性质',
                content: $('#spaceCharacterForm').html(),
                success: function () {
                    form.val('spaceCharacterForm', data);
                    // 表单提交事件
                    form.on('submit(spaceCharacterFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/system/admin/spaceCharacter/update' : 'enterprise/system/admin/spaceCharacter/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('spaceCharacterTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        //房间类型弹窗
        function showRoomType(data){
            console.log(data.data.id)
            admin.open({
                type: 1,
                area: '1000px',
                offset: '65px',
                title:  '房型管理',
                content: $('#roomTypeWindow').html(),
                success:function () {
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#roomTypeTable',
                        url: config.base + 'enterprise/system/admin/roomType/list?spaceCharacterId='+data.data.id,
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{field: 'sortNumber', sort: true, title: '序号'}
                            ,{field: 'code', sort: true, title: '房间类型编码'}
                            ,{field: 'name', sort: true, title: '房间类型名称'}
                            ,{field: 'peopleNumber', sort: true, title: '可以驻人数'}
                            ,{field: 'spaceCharacterName', sort: true, title: '空间性质'}
                            ,{field: 'deleted', sort: true, title: '是否启用',templet:function (d) {
                                    if(d.deleted){
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.deleted+'" lay-filter="deleted"  lay-text="启用|停用"/>';
                                    }else{
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="deleted"   lay-text="启用|停用"/>';
                                    }
                                }}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                            ,{align: 'center', toolbar: '#roomTypeTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {

                        }
                    });

                    // 工具条点击事件
                    table.on('tool(roomTypeTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'roomTypeEdit') { //修改
                            showRoomTypeEditModel(data);
                        } else if (obj.event === 'roomTypeDel') { // 删除
                            doRoomTypeDelete(obj);
                        }
                    });

                    // 添加按钮点击事件
                    $('#roomTypeBtnAdd').click(function () {
                        showRoomTypeEditModel();
                    });

                    form.on('switch(deleted)',function(obj){
                        if(obj.elem.checked){
                            roomTypeEnable(obj.value)
                        }else{
                            roomTypeDisable(obj.value)
                        }
                    })

                    function roomTypeEnable(id){
                        admin.req('enterprise/system/admin/roomType/enable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload('systemWorkmanshipTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }

                    function roomTypeDisable(id){
                        admin.req('enterprise/system/admin/roomType/disable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload('systemWorkmanshipTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }
                    // 显示编辑弹窗
                    function showRoomTypeEditModel(data) {

                        admin.req('enterprise/system/admin/spaceCharacter/list', {limit:9999}, function (res) {
                            if (res.code == 200) {
                                if (res.data.length > 0){
                                    $.each(res.data,function (index,item) {
                                        $("#spaceCharacterId").append(new Option(item.name,item.id));
                                    })
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        admin.open({
                            type: 1,
                            area: '700px',
                            offset: '65px',

                            title: data ? '修改房间类型' : '添加房间类型',
                            content: $('#roomTypeForm').html(),
                            success: function () {
                                form.val('roomTypeForm', data);
                                // 表单提交事件
                                form.on('submit(roomTypeFormSubmit)', function (d) {
                                    layer.load(2);
                                    admin.req(data ? 'enterprise/system/admin/roomType/update' : 'enterprise/system/admin/roomType/add', d.field, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            table.reload('roomTypeTable');
                                            layer.closeAll('page');
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, data ? 'PUT' : 'POST');
                                    return false;
                                });
                            }
                        });
                    }

                    // 删除
                    function doRoomTypeDelete(obj) {
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/system/admin/roomType/' + obj.data.id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    obj.del();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'DELETE');
                        });
                    }
                }
            })
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/spaceCharacter/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#spaceCharacterBtnExport').click(function () {
            var checkRows = table.checkStatus('spaceCharacterTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>